import java.rmi.registry.Registry;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Like
 * Date: 2021-07-13
 * Time: 16:26
 */



public class RegSingleton{
    //使用一个map来当注册表
    static private HashMap registry=new HashMap();
    //静态块，在类被加载时自动执行，把RegistSingleton自己也纳入容器管理
    static{
        RegSingleton rs=new RegSingleton();
        registry.put(rs.getClass().getName(),rs);
    }
    //受保护的默认构造函数，如果为继承关系，则可以调用，克服了单例类不能为继承的缺点
    protected RegSingleton(){}
    //静态工厂方法，返回此类的唯一实例
    public static RegSingleton getInstance(String name) {
        if (name == null) {
            name = "RegSingleton";
        }
        if (registry.get(name) == null) {
            try {
                registry.put(name, Class.forName(name).newInstance());
            } catch (ClassNotFoundException | IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }

            finally {

            }
            }
            return (RegSingleton) registry.get(name);
        }
    }